﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DatingWebSite.DatingService;

namespace DatingWebSite.Pages
{
    public partial class join : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //2010 - 1000 (1910)
                for (int year = DateTime.Today.Year; year > (DateTime.Today.Year - 100); year--)
                    drpYear.Items.Add(year.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                lblErrMessage.Text = string.Empty; //Clear the error message.

                //Check for the validations
                if ((txtEmail.Text == string.Empty) || (txtPassword.Text == string.Empty)
                    || (txtUserName.Text == string.Empty) || chkTerms.Checked == false)
                {

                    lblErrMessage.Text = "Please fill in all required fields and try again...";
                }

                else
                {
                    //Add profile. 

                    MemberProfile objProfile = new MemberProfile();
                    objProfile.MySelf = drpMySelf.Text;
                    objProfile.Seeking = drpSeeking.Text;
                    objProfile.EmailId = txtEmail.Text;
                    objProfile.UserName = txtUserName.Text;
                    objProfile.Passwrd = txtPassword.Text;
                    
                    DateTime Dob;
                    DateTime.TryParse((drpMonth.Text + "/" + drpDay.Text + "/" + drpYear.Text).ToString(), out Dob);
                    objProfile.DOB = Dob;
                    objProfile.TenantID = Convert.ToInt32(Session["TenentID"]);

                    objProfile.CreatedOn = DateTime.Today;
                    objProfile.LastLoginOn = DateTime.Today;


                    DatingService.DatingService objUpdate = new DatingWebSite.DatingService.DatingService();
                    int memberProfileID = objUpdate.MemberProfileInsert(objProfile, Session["TenentID"].ToString());

                    if (memberProfileID <= 0)
                    {
                        lblErrMessage.Text = "There seems to be a technical issue while registring you in our system. Administrator has been informed. <br/> Please register later.";
                    }
                    else
                    {
                        String msgbody = "";
                        bool flag;
                        if (ConfigurationManager.AppSettings["EMAIL_CONTENT_FILE"] != "")
                        {
                            msgbody = ReadFromFile(ConfigurationManager.AppSettings["EMAIL_CONTENT_FILE"]);

                            // msgbody = msgbody.Replace("##NAME##", Common.Common.InitCap(strFirstName));                           

                        }

                        flag = Common.Common.SendMail(txtEmail.Text.ToString(), ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["STRSUBJECT"], msgbody);

                        if (flag)
                            Response.Redirect("thanks_regs.aspx");
                        else
                            lblErrMessage.Text = " There was an error inserting profile. Please contact administrator. ";


                        Response.Redirect("thanks_regs.aspx");
                    }
                }

            }
            catch (Exception err)
            {
                lblErrMessage.Text = " There was an error inserting profile. Please contact administrator";
            }
        }

        #region "Read Mail Format HTML File"
        public string ReadFromFile(string path)
        {
            StreamReader sr;
            string s = "";
            try
            {
                string filepath = base.Server.MapPath(path);
                sr = File.OpenText(filepath);
                s = sr.ReadToEnd();
                sr.Close();
                sr = null;
            }
            catch (Exception ex)
            {
                //
            }
            finally
            {
                sr = null;
            }
            return (s);
        }
        #endregion  
    }
}
